package com.jsj.service;

import com.jsj.dao.CourseDao;
import com.jsj.dao.SclassDao;
import com.jsj.dao.TeacherDao;
import com.jsj.domain.Course;
import com.jsj.domain.StuClass;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CourseService {
    private CourseDao courseDao;
    private SclassDao sclassDao;
    private TeacherDao teacherDao;

    @Autowired
    public void setCourseDao(CourseDao courseDao) {
        this.courseDao = courseDao;
    }

    @Autowired
    public void setSclassDao(SclassDao sclassDao) {
        this.sclassDao = sclassDao;
    }

    @Autowired
    public void setTeacherDao(TeacherDao teacherDao) {
        this.teacherDao = teacherDao;
    }

    public Integer insert(Course course) {
        return courseDao.insert(course);
    }

    public List<Course> query() {
        List<Course> courses = courseDao.query();
        if (courses != null) {
            for (Course course : courses) {
                course.setSclass(sclassDao.query(course.getClassId()));
                course.setTeacher(teacherDao.queryByUserName(course.getTeacherId()));
            }
        }
        return courses;
    }

    public List<Course> queryByTeacher(String teacherId) {
        List<Course> courses = courseDao.queryByTeacher(teacherId);
        if (courses != null) {
            for (Course course : courses) {
                course.setSclass(sclassDao.query(course.getClassId()));
                course.setTeacher(teacherDao.queryByUserName(course.getTeacherId()));
            }
        }
        return courses;
    }

    public List<Course> queryByStudent(String studentId) {
        List<Course> courses = courseDao.queryByStudent(studentId);
        if (courses != null) {
            for (Course course : courses) {
                course.setSclass(sclassDao.query(course.getClassId()));
                course.setTeacher(teacherDao.queryByUserName(course.getTeacherId()));
            }
        }
        return courses;
    }
}
